$(function() {
         
    //初始化用户数据
    init();

    
    var discuss_form = $("#discuss_form");
    //用户评论
    $(document).on("click",".subComment",function(e){
        $(".lv-comment").show();
        var $this = $(this);
        var parent = $this.parent().parent().parent();
        var this_id =parent.children("input[name='this_id']:hidden").val(); //当前id
        var user_name = parent.children("input[name='user_name']:hidden").val(); //用户姓名
        var user_img = parent.children("input[name='user_img']:hidden").val();    //用户的图片
        var subContent =$.trim($this.parent().parent().children("#substitutor_content").val());
        var loading = layer.msg('加载中...', {icon: 16});
        var str ="";
        var isUserlvComm = $this.attr("data-id");

        if(user_img.length < 1){
                user_img.attr("value","/img/case/defaultAvatar.png");
        }
        if(subContent.length < 1){
            layer.msg('提交的内容不能为空！');
        }else{

            if(isUserlvComm == 0){
                $.post(discuss_form[0].action, discuss_form.serialize(),
               function(data){
                    
               }).error(function(data) {
                    str+='<li class="comments-item bor3">'
                        str+='<div class="comments-item-bd">'
                            str+='<div class="ui_avatar">'
                                str+='<a href="javascript:void(0)" target="_self"><img src="'+url+user_img+'" /></a>'
                            str+='</div>'
                            str+='<div class="comments-content">'
                                str+='<a class="nickname" href="javascript:void(0);" target="_self">'+user_name+'</a>'
                                str+='<span>'+subContent+'</span>'
                                str+='<div class="comments-op">'
                                    str+='<span class="c_tx3 ui_mr10">'+getDateTime()+'</span>'
                                    str+='<a href="javascript:void(0);" class="lv-comment-reply c_tx">回复</a>'
                                str+='</div>'
                            str+='</div>'
                            str+='<div class="lv-comments-sub"></div>'
                        str+='</div>'
                    str+='</li>';
                    $(".comments-list ul").append(str);
                    layer.close(loading);
                    //清空form的数据
                    $(':input','#discuss_form').not('textarea :submit, :reset, :hidden').val(''); 
                    pages();

               });
            }else{
                var replyName =parent.parent().parent().parent().find(".nickname").eq(0).text();
                var actionUrl = parent[0].action;

                $.post(actionUrl, parent.serialize(),
               function(data){
                    
               }).error(function(data) {
                    str+='<ol>'
                       str+='<li class="comments-item bor3">'
                            str+='<div class="comments-item-bd">'
                                 str+='<div class="ui_avatar">'
                                    str+='<a href="javascript:void(0);" target="_self"><img src="'+url+user_img+'" /></a>'
                                 str+='</div>'
                                 str+='<div class="comments-content">'
                                      str+='<a class="nickname" href="javascript:void(0);" target="_self">'+user_name+'</a>'
                                      str+='<span><span> 回复 </span><a class="nickname" href="" target="_self">'+replyName+'</a></span> : '
                                      str+='<span>'+subContent+'</span>'
                                      str+='<div class="comments-op">'
                                           str+='<span class="c_tx3 ui_mr10">'+getDateTime()+'</span>'
                                           str+='<a class="c_tx comments-sub-reply" href="javascript:void(0);" commentboxid="4">回复</a>'
                                      str+='</div>'
                                 str+='</div>'
                            str+='</div>'
                        str+='</li>'
                    str+='</ol>';

                    parent.parent().parent().parent().append(str);
                    layer.close(loading);
                    parent.parent().remove();
                    //清空form的数据
                    $(':input','#discuss_form').not('textarea  :submit, :reset, :hidden').val(''); 
                    pages();
               });
                
            }
        }
        stopDefault(e);
    });

    //律师回复评论
    $(document).on("click",".lv-comment-reply",function(e){
        var lvInfoJson ={
                                "id":"1",
                                "name":"刘心",
                                "img":"/img/case/lvLawyer.png"
                              }; 
        $(".lv-poster-bd").remove();
        var $this = $(this);
        var str ="";
        str+='<div class="lv-poster-bd">'
                str+='<form id="discuss_form" method="post" action="'+discuss_form[0].action+'">'
                    str+='<input type="hidden" name="this_id" value="'+lvInfoJson.id+'">'
                    str+='<input type="hidden" name="user_name" value="'+lvInfoJson.name+'">'
                    str+='<input type="hidden" name="user_img" value="'+lvInfoJson.img+'">'
                    str+='<a class="ds-avatar" href="javascript:void(0);"><img src="'+url+lvInfoJson.img+'" alt=""></a>'
                    str+='<div class="content_content">'
                        str+='<textarea placeholder="说点什么吧...." id="substitutor_content" rows="10" cols="50" class="itxtarea" name="content"></textarea>'
                        str+='<pre class="ds-hidden-text"></pre>'
                        str+='<div class="clearfix ds-post-toolbar">'
                            str+='<a href="javascript:void(0);" data-id="1" type="button" class="inpbg subComment" value="">发表评论</a>'
                        str+='</div>'
                    str+='</div>'
                str+='</form>'
           str+='</div>';

           var thislayer =  $this.parent().parent().siblings(".lv-comments-sub")
           thislayer.append(str);

        stopDefault(e);
    });

    //用户回复评论
    $(document).on("click",".comments-sub-reply",function(e){
        var userInfoJson ={
                                "id":"2",
                                "name":"刘心2",
                                "img":"/img/case/defaultAvatar.png"
                              }; 
        $(".lv-poster-bd").remove();
        var $this = $(this);
        var str ="";
        //获取律师的姓名
        var userName = $this.parent().parent().find(".nickname").eq(0).text();
        str+='<div class="lv-poster-bd">'
                str+='<form id="discuss_form" method="post" action="'+discuss_form[0].action+'">'
                    str+='<input type="hidden" name="this_id" value="'+userInfoJson.id+'">'
                    str+='<input type="hidden" name="user_name" value="'+userInfoJson.name+'">'
                    str+='<input type="hidden" name="user_img" value="'+userInfoJson.img+'">'
                    str+='<a class="ds-avatar" href="javascript:void(0);"><img src="'+url+userInfoJson.img+'" alt=""></a>'
                    str+='<div class="content_content">'
                        str+='<textarea placeholder="说点什么吧...." id="substitutor_content" rows="10" cols="50" class="itxtarea" name="content"></textarea>'
                        str+='<pre class="ds-hidden-text"></pre>'
                        str+='<div class="clearfix ds-post-toolbar">'
                            str+='<a href="javascript:void(0);" data-id="1" type="button" class="inpbg subComment" value="">发表评论</a>'
                        str+='</div>'
                    str+='</div>'
                str+='</form>'
           str+='</div>';
           var thislayer =  $this.parent().parent().parent();
           thislayer.append(str);
           var altName = $this.parent().parent().siblings(".lv-poster-bd").children().children().children(".ds-hidden-text");
            $("#commReply").focus();

        stopDefault(e);
    });
    
    //在输入评论的时候把值付给 文本
    $(document).on( "keyup", "form textarea", function(e) {
        var $this = $(this);
        var val = $.trim($this.val());
        $this.siblings(".ds-hidden-text").text(val)
        stopDefault(e);
    })



    
});

    //初始化用户数据的方法
    function init(){
        var userInfoJson ={
                                "id":"0",
                                "name":"小刘心",
                                "img":"/img/defaultmsg1.png"
                              };          
        $("input[name='this_id']:hidden").attr("value",userInfoJson.id);
        $("input[name='user_name']:hidden").attr("value",userInfoJson.name);
        $("input[name='user_img']:hidden").attr("value",userInfoJson.img); 
    }

//获取当前的时间方法 不追加0
/*function getDateTime(){
    var mydate = new Date();
    var str = "" + mydate.getFullYear()+"-";
    str += (mydate.getMonth()+1+"-");
    str += mydate.getDate()+" ";
    str += mydate.getHours()+":";
    str += mydate.getMinutes();
    return str;
}*/

//获取当前的时间的方法 追加0
function getDateTime()
{ 
    var date = new Date();
    var year = date.getFullYear();       //年
    var month = date.getMonth() + 1;     //月
    var day = date.getDate();            //日
/*    var hh = date.getHours();            //时
    var mm = date.getMinutes();          //分*/
    var str = year + "-";
    if(month < 10)
        str += "0";
    str += month + "-";
    if(day < 10)
        str += "0";
    str += day + " ";
   /* if(hh < 10)
        str += "0";
    str += hh + ":";
    if (mm < 10) str += '0'; 
    str += mm; */
    return(str); 
} 

